package com.itheima.pinda.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.pinda.DTO.base.GoodsTypeDto;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.base.PdGoodsType;
import com.itheima.pinda.entity.truck.PdTruckTypeGoodsType;
import com.itheima.pinda.service.base.IPdGoodsTypeService;
import com.itheima.pinda.service.truck.IPdTruckTypeGoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 货物类型管理
 *
 * @progam:pindar
 * @author:GJS
 * @Time:2022/5/2 18:53
 */
@RestController
@RequestMapping("base/goodsType")
@Api(tags = "货物类型管理")
public class GoodsTypeController {

    @Autowired
    private IPdGoodsTypeService goodsTypeService;

    @Autowired
    private IPdTruckTypeGoodsTypeService truckTypeGoodsTypeService;

    /**
     * 新增货物类型，同时要关联车辆类型
     *
     * @param goodsTypeDto 货物类型信息
     * @return 货物类型信息
     */
    @PostMapping("")
    @ApiOperation(value = "添加货物类型")
    public GoodsTypeDto saveGoodsType(@Validated @RequestBody GoodsTypeDto goodsTypeDto) {
        PdGoodsType pdGoodsType = new PdGoodsType();
        //goodsTypeDto中与pdGoodsType中相同的属性都会被替换，不管是否有值；
        BeanUtils.copyProperties(goodsTypeDto, pdGoodsType);
        //保存货物类型信息到货物类型表
        pdGoodsType = goodsTypeService.saveGoodsType(pdGoodsType);
        //货物类型id
        String goodsTypeId = pdGoodsType.getId();
        //报存货物类型和车辆类型关联信息到关联表
        List<String> truckTypeIds = goodsTypeDto.getTruckTypeIds();
        if (!CollectionUtils.isEmpty(truckTypeIds) && truckTypeIds.size() > 0) {
            List<PdTruckTypeGoodsType> list = truckTypeIds.stream().map(truckTypeId -> {
                PdTruckTypeGoodsType pdTruckTypeGoodsType = new PdTruckTypeGoodsType();
                //货物类型id
                pdTruckTypeGoodsType.setGoodsTypeId(goodsTypeId);
                //车辆id
                pdTruckTypeGoodsType.setTruckTypeId(truckTypeId);
                return pdTruckTypeGoodsType;
            }).collect(Collectors.toList());
            //批量保存货物类型和车辆类型的关联信息
            truckTypeGoodsTypeService.batchSave(list);
        }
        //返回添加后的id
        BeanUtils.copyProperties(pdGoodsType, goodsTypeDto);
        return goodsTypeDto;
    }

    /**
     * 根据id货物类型
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询货物类型")
    public GoodsTypeDto findById(@PathVariable("id") String id) {
        //根据id货物类型
        PdGoodsType pdGoodsType = goodsTypeService.getById(id);

        GoodsTypeDto goodsTypeDto = new GoodsTypeDto();
        BeanUtils.copyProperties(pdGoodsType, goodsTypeDto);

        //查询货物类型关联的车辆类型的id
        List<PdTruckTypeGoodsType> list = truckTypeGoodsTypeService.findAll(null, pdGoodsType.getId());

        if (!CollectionUtils.isEmpty(list) && list.size() > 0) {
            List<String> truckTypeId = list.stream().map(pdTruckTypeGoodsType ->
                    pdTruckTypeGoodsType.getTruckTypeId()
            ).collect(Collectors.toList());
            goodsTypeDto.setTruckTypeIds(truckTypeId);
        }

        return goodsTypeDto;
    }

    /**
     * 查询所有货物类型
     *
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有货物类型")
    public List<GoodsTypeDto> findAll() {
        List<PdGoodsType> goodsTypeList = goodsTypeService.findAll();
        if (!CollectionUtils.isEmpty(goodsTypeList) && goodsTypeList.size() > 0) {
            List<GoodsTypeDto> goodsTypeDtoList = goodsTypeList.stream().map(pdGoodsType -> {
                GoodsTypeDto goodsTypeDto = new GoodsTypeDto();
                BeanUtils.copyProperties(pdGoodsType, goodsTypeDto);
                return goodsTypeDto;
            }).collect(Collectors.toList());
            return goodsTypeDtoList;
        }
        return null;
    }

    /**
     * 分页查询货物类型
     *
     * @param page
     * @param pageSize
     * @param name          required = false 不是必须的
     * @param truckTypeId   required = false 不是必须的
     * @param truckTypeName required = false 不是必须的
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询货物类型")
    public PageResponse<GoodsTypeDto> findByPage(
            @RequestParam(name = "page") Integer page,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "truckTypeId", required = false) String truckTypeId,
            @RequestParam(name = "truckTypeName", required = false) String truckTypeName
    ) {
        //分页查询
        IPage<PdGoodsType> goodsTypePage = goodsTypeService.findByPage(page, pageSize, name, truckTypeId, truckTypeName);

        //获取记录 根据条件查到的多个货物类型
        List<PdGoodsType> goodsTypePageRecords = goodsTypePage.getRecords();

        if (!CollectionUtils.isEmpty(goodsTypePageRecords) && goodsTypePageRecords.size() > 0) {
            List<GoodsTypeDto> goodsTypeDtoList = goodsTypePageRecords.stream().map(goodsType -> {
                //根据货物类型查车辆id
                List<PdTruckTypeGoodsType> truckTypeGoodsType = truckTypeGoodsTypeService.findAll(null, goodsType.getId());
                //转换类型，方便车辆id的赋值
                List<String> truckTypeIds = truckTypeGoodsType.
                        stream().map(pdTruckTypeGoodsType -> pdTruckTypeGoodsType.getTruckTypeId()).collect(Collectors.toList());

                GoodsTypeDto goodsTypeDto = new GoodsTypeDto();
                BeanUtils.copyProperties(goodsType, goodsTypeDto);
                goodsTypeDto.setTruckTypeIds(truckTypeIds);
                return goodsTypeDto;
            }).collect(Collectors.toList());

            PageResponse<GoodsTypeDto> goodsTypeDtoPageResponse = PageResponse.<GoodsTypeDto>builder().items(goodsTypeDtoList).
                    counts(goodsTypePage.getTotal()).page(page).pages(goodsTypePage.getPages()).pagesize(pageSize).build();

            return goodsTypeDtoPageResponse;
        }

        return null;
    }
}
